package main

import (
    "bufio"
    "context"
    "errors"
    "fmt"
    "gitee.com/gober/prototest/proto"
    "net"
    "os"
    //"time"
    "google.golang.org/grpc"
)

type IMessage struct {
    proto.UnimplementedMessageServiceServer
    proto.UnimplementedPingServiceServer
}

func (s *IMessage) Message(ctx context.Context, request *proto.MessageRequest) (*proto.MessageResponse, error) {
    fmt.Printf("客户端 %d  -> 说: %s\r\n", request.Id, request.Msg)
    input := bufio.NewScanner(os.Stdin)
    fmt.Printf("输入回复：")
    if !input.Scan() {
        return &proto.MessageResponse{
            Id:   1000,
            Code: 2,
        }, errors.New("输入有误！")
    }
    //fmt.Printf("输入的内容：%s\r\n", input.Text())
    return &proto.MessageResponse{
        Id:   1000,
        Code: 0,
        Msg:  input.Text(),
    }, nil
}

func (s *IMessage) Ping(ctx context.Context, request *proto.PingRequest) (*proto.PingResponse, error) {
    fmt.Println("心跳")
    return &proto.PingResponse{}, nil
}

func main() {
    fmt.Println("服务器启动......")
    lsocket, err := net.Listen("tcp", ":6666")

    if err != nil {
        fmt.Printf("tcp listen error : %s\r\n", err.Error())
        return
    }
    fmt.Println("端口监听中 ️。。。。")
    rpcs := grpc.NewServer()

    proto.RegisterMessageServiceServer(rpcs, &IMessage{})
    proto.RegisterPingServiceServer(rpcs, &IMessage{})

    fmt.Println("gRPC 服务端启动，等待连接 ️。。。。")
    err = rpcs.Serve(lsocket)
    fmt.Println("走到这里了？？？")
    if err != nil {
        fmt.Printf("rpcs.Serve Error:%s\r\n", err.Error())
    }
}

//
//func main() {
//    Start()
//}
